﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WooCoo.Extension
{
    /// <summary>
    /// Object扩展类
    /// </summary>
    public static class ObjectExtensions
    {

        #region 转换为整数[异常或非法均返回默认值]
        /// <summary>
        /// 转换为整数[异常或非法均返回默认值]
        /// </summary>
        /// <param name="obj">参数</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="maxValueAllowed">允许的最大值</param>
        /// <returns></returns>
        public static int ToInt32(this object obj, int defaultValue, int maxValueAllowed = Int32.MaxValue)
        {
            if (obj == null)
                return defaultValue;

            if (obj.GetType() == typeof(int))
                return (int)obj;
            //字符串处理
            var resultString = obj.ToString().TrimStart('0');

            //判断字符串合法性
            if (!Regex.IsMatch(resultString, @"^(-|\+)?\d+(\.\d+)?$"))
            {
                return defaultValue;
            }

            //判断整数部分长度超过11位(负号加一位)
            if (resultString.Length > 11 && resultString.Split('.')[0].Length > 11)
            {
                return defaultValue;
            }
            //Int32.TryParse()

            //处理四舍五入，及最大值最小值判断
            double temp = double.Parse(resultString);

            //如果超过允许的最大值
            if (temp > maxValueAllowed)
            {
                return maxValueAllowed;
            }

            //判断是否大于INT32最大值
            if (temp > int.MaxValue || temp < int.MinValue)
            {
                return defaultValue;
            }

            //结果
            return int.Parse(Math.Round(temp, 0).ToString());
        }
        #endregion

        #region 转换为整数
        /// <summary>
        /// 转换为整数
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public static int ToInt32(this object obj)
        {
            return obj.ToInt32(0);
        }
        #endregion

        #region 转换为decimal
        /// <summary>
        /// 转换为decimal
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this object obj, bool isRounding = true)
        {
            if (obj == null)
                return 0;

            //字符串处理
            var resultString = obj.ToString().TrimStart('0');

            //判断字符串合法性
            return !Regex.IsMatch(resultString, @"^(-|\+)?\d+(\.\d+)?$") ? 0 : decimal.Parse(resultString);

            //转换
        }
        #endregion

        #region 转换为DateTime
        /// <summary>
        /// 转换为DateTime
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime ToDate(this object obj)
        {
            if (obj == null)
            {
                return new DateTime(1900, 1, 1);
            }
            try
            {
                return Convert.ToDateTime(obj.ToString());
            }
            catch (Exception ex)
            {
                //LoggerHelper.OutError("转换日期发生异常，返回默认值！", ex);
                return new DateTime(1900, 1, 1);
            }
        }
        #endregion

        #region 转换为字符串
        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToStr(this object obj, string defaultValue = "")
        {
            return obj == null ? defaultValue : obj.ToString();
        }
        #endregion

        #region 转换为Bool
        /// <summary>
        /// 转换为Bool
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static bool ToBool(this object obj, bool defaultValue = false)
        {
            if (obj == null)
            {
                return defaultValue;
            }
            return string.Equals(obj.ToString(), "true", StringComparison.InvariantCultureIgnoreCase) || string.Equals(obj.ToString(), "1");
        }
        #endregion
    }
}
